<template>
  <div style="padding: 10px">

    <!--    搜索区域-->
    <div style="margin: 10px 0">
      <el-button type="info" @click="add">新增</el-button>
    </div>

    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
    <el-table-column
        prop="id"
        label="ID"
        sortable
    >
    </el-table-column>
    <el-table-column
        prop="title"
        label="标题">
    </el-table-column>
    <el-table-column
        prop="author"
        label="作者">
    </el-table-column>
      <el-table-column
          prop="createTime"
          label="出版时间">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button @click="details(scope.row)" size="mini">详情</el-button>
          <el-button @click="handleEdit(scope.row)" size="mini">编辑</el-button>
          <el-popconfirm title="确定删除此条数据?" icon-color="red" cancel-button-type="info" confirm-button-type="danger"
          @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="mini" type="info" >删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
  </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>

      <el-dialog title="文章" :visible.sync="dialogVisible" width="50%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="标题">
            <el-input v-model="form.title" style="width: 50%"></el-input>
          </el-form-item>

          <div id="div1"></div>
<!--          <el-form-item label="内容">
            <el-input v-model="form.author" style="width: 80%"></el-input>
          </el-form-item>-->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" >取 消</el-button>
          <el-button type="info" @click="save">确 定</el-button>
        </span>
      </el-dialog>

      <el-dialog title="详情" :visible.sync="vis" style="min-height: 1000px" width="50%">
        <el-card>
          <div v-html="detail.content"></div>
        </el-card>
      </el-dialog>

    </div>

  </div>

</template>

<script>

import E from 'wangeditor'
import request from "../utils/request";
import Header2 from "../components/Header2";
import Aside2 from "../components/Aside2";

let editor;

export default {
  name: 'Essay',
  components: {
    Aside2,
    Header2

  },
  data()
  {
    return{
      form:{},
      dialogVisible:false,
      search:'',
      currentPage4:1,
      pageSize:10,
      total:0,
      tableData: [],
      vis:false,
      detail:{}
    }
  },
  created() {
    this.load()
  },
  methods:{
    details(row){
      this.detail = row
      this.vis = true
    },
    load()
    {
      request.get("/essay",{
        params:{
        pageNum: this.currentPage,
            pageSize: this.pageSize,
          search:this.search
        }
    }).then(res=>{
      console.log(res)
        this.tableData = res.data.records
        this.total = res.data.records.length
    })
    },
    add()
    {
        this.dialogVisible = true
        this.form = {}

        this.$nextTick(()=>{
          //关联弹窗了吗的div。new 一个editor对象
          editor = new E('#div1')

          //配置server接口地址
          editor.config.uploadImgServer = 'http://localhost:9090/files/eidtor/upload'
          editor.config.uploadFileName = "file";
          editor.create()
        })
    },
    handleDelete(id)
    {
      console.log(id)
      request.delete("/essay/" + id).then(res=>{
        if(res.code === '0')
        {
          this.$message({
            type:"success",
            message:"删除成功"
          })
        } else {
          this.$message({
            type:"error",
            message:res.msg
          })
        }
        this.load()
      })
    },
    save()
    {
      this.form.content = editor.txt.html()//获取编辑器里面的值 ，放在实体中
        if(this.form.id) //更新
        {
          request.put("/essay",this.form).then(res=>{
            console.log(res)
            if(res.code === '0')
            {
              this.$message({
                type:"success",
                message:"更新成功"
              })
            } else {
              this.$message({
                type:"error",
                message:res.msg
              })
            }
             this.load()//刷新表格的数据
             this.dialogVisible = false
          })
        } else {//新增
          let userStr = sessionStorage.getItem("user") ||"{}"
          let user = JSON.parse(userStr)
          this.form.author = user.username
          request.post("/essay", this.form).then(res => {
            console.log(res)
            if(res.code === '0')
            {
              this.$message({
                type:"success",
                message:"新增成功"
              })
            } else {
              this.$message({
                type:"error",
                message:res.msg
              })
            }
            this.load()//刷新表格的数据
            this.dialogVisible = false
          })
        }
    },
    handleEdit(row){
         this.form = JSON.parse(JSON.stringify(row))
         this.dialogVisible = true

      if (!editor) {
        this.$nextTick(() => {
          //关联弹窗了吗的div。new 一个editor对象
          editor = new E('#div1')
          editor.create()
          editor.txt.html(row.content)
        })
      }


    },
    handleSizeChange(pageSize)//改变当前每页的个数触发
    {
      this.pageSize = pageSize
        this.load()
    },
    handleCurrentChange(pageNum){ //改变当前页码触发
      this.currentPage4 = pageNum
       this.load()
    }
  }

}
</script>
